

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "support.h"
#include "gsim.h"
#include "window.h"
#include "fileops.h"
#include "config.h"

/*
 * Standard gettext macros.
 */
#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

#ifdef G_OS_WIN32
gchar *package_prefix = PACKAGE_PREFIX;
gchar *package_data_dir = PACKAGE_DATA_DIR;
gchar *package_locale_dir = PACKAGE_LOCALE_DIR;
gchar *package_help_dir = PACKAGE_HELP_DIR;
gchar *package_pixmaps_dir = PACKAGE_PIXMAPS_DIR;
#endif

/*############### Static functions ###############################*/
static void gsim_print_usage(void);
static void gsim_print_version(void);
static void gsim_init(int argc, char *argv[]);

/*############### Print the usage information ####################*/ 
static void gsim_print_usage(void)
{
	g_print("Usage: gsim85 [options] [file]\n\n");
	g_print("Options:\n");
	g_print("  -h, --help       Display command line options\n");
	g_print("  -v, --version    Display version information\n");
	g_print("\n");
	exit(0);
}


/*############## Print version information #####################*/
static void gsim_print_version(void)
{
	g_print("GSim85 %s\n", GSIM85_VERSION);
	exit(0);
}


/*########## Initilise all components of applicatin###########*/
static void gsim_init(int argc, char *argv[])
{
	int c ;
	
	static struct option long_opts[ ] = {
		{ "help",		no_argument,	NULL,	'h' },
		{ "version",	no_argument,	NULL,	'v' }
	};

	while ((c = getopt_long(argc, argv, "hv", long_opts, NULL)) != -1) 
	{
		switch (c) 
		{
			case 'h':
				gsim_print_usage( );
				break;
		 	case 'v':
				gsim_print_version();
				break;
			case '?':
				gsim_print_usage( );
				break;
			default :
				exit(0);
		}
	}
	load_config_file();
	gtk_rc_parse(gsim_files.style);
	gsim_create_appwindow( );             /*create application GUI*/
	
	/*open other files specified on the command line*/
	gint i;
	for(i=optind;i<argc;i++)
		open_file_dialog_ok(NULL,argv[i]);
	
	return ;
}
	
/*#################### All stuff start from here ####################*/
int main( int argc , char **argv )
{
#ifdef G_OS_WIN32
  package_prefix = g_win32_get_package_installation_directory (NULL, NULL);
  package_data_dir = g_build_filename (package_prefix, "share", NULL);
  package_locale_dir = g_build_filename (package_prefix, "share", "locale", NULL);
  package_help_dir = g_build_filename (package_prefix, "share", "help", NULL);
  package_pixmaps_dir = g_build_filename (package_prefix, "share", "pixmaps", NULL);
#endif

#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif
	
	gtk_set_locale( ) ;                                      
	
	g_thread_init( NULL );                             
	gdk_threads_init( ) ;
	
	gtk_init( &argc , &argv ) ;                          
	gsim_init( argc , argv ) ;                            

	gdk_threads_enter( ) ;  	
	gtk_main( ) ;
	gdk_threads_leave( );

#ifdef G_OS_WIN32
  g_free (package_prefix);
  g_free (package_data_dir);
  g_free (package_locale_dir);
#endif

	return 0;
}

#ifdef G_OS_WIN32
#ifdef _MSC_VER
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
  return main (__argc, __argv);
}
#endif
#endif
